package com.lehecai.core.api.message;

import com.lehecai.core.YesNoStatus;
import com.lehecai.core.api.AbstractApiResultBean;
import com.lehecai.core.util.CoreDateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by zhangzq on 14-4-11.
 */
public class Message extends AbstractApiResultBean {

    public static final String QUERY_MID = "mid";            //消息id
    public static final String QUERY_CLASSES = "classes";    //一级
    public static final String QUERY_TYPE = "type";          //二级
    public static final String QUERY_PUBLISHAT = "publish_at";  //发布时间
    public static final String QUERY_ISPUSH = "is_push";  //是否推送
    public static final String QUERY_LAUNCHUSER = "launch_user";  // 接收用户ids

    public static final String SET_MID = "mid";
    public static final String SET_TITLE = "title";
    public static final String SET_CONTENT = "content";
    public static final String SET_TIMEOUTAT = "timeout_at";
    public static final String SET_LUNCHUSER = "launch_user";
    public static final String SET_CLASSES = "classes";
    public static final String SET_TYPE = "type";
    public static final String SET_UID = "uid";

    private String mid;                    //消息id
    private String title;                   //消息标题
    private String content;	                //内容
    private String launchUser;	            //接受用户UID
    private Date publishAt;                //发布时间
    private Date timeoutAt;                //有效时间
    private MessageType classes;			//消息大类
    private MessageDetailType type;			//消息小类
    private YesNoStatus isPush;             //是否推送
    private ReceiveType receiveType;
    private String userName;

    public static Message convertFromJSONObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        Message message = new Message();
        message.mid = getString(object, "mid");
        message.title = getString(object, "title");
        message.content = getString(object, "content");
        message.launchUser = getString(object, "launch_user");
        message.publishAt = CoreDateUtils.parseDate(("".equals(getString(object, "publish_at")) || "0000-00-00 00:00:00".equals(getString(object, "publish_at"))) ? null : getString(object, "publish_at"), CoreDateUtils.DATETIME);
        message.timeoutAt = CoreDateUtils.parseDate(("".equals(getString(object, "timeout_at")) || "0000-00-00 00:00:00".equals(getString(object, "timeout_at"))) ? null : getString(object, "timeout_at"), CoreDateUtils.DATETIME);
        message.classes = MessageType.getItem(object.getInt("classes"));
        message.type = MessageDetailType.getItem(object.getInt("type"));
        message.isPush = YesNoStatus.getItem(object.getInt("is_push"));
        message.userName = getString(object, "userName");
        return message;
    }

    public static List<Message> convertFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        List<Message> list = new ArrayList<Message>();
        for (Iterator<?> iterator = array.iterator(); iterator.hasNext();) {
            JSONObject object = (JSONObject) iterator.next();
            list.add(convertFromJSONObject(object));
        }
        return list;
    }

    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLaunchUser() {
        return launchUser;
    }

    public void setLaunchUser(String launchUser) {
        this.launchUser = launchUser;
    }

    public Date getPublishAt() {
        return publishAt;
    }

    public void setPublishAt(Date publishAt) {
        this.publishAt = publishAt;
    }

    public Date getTimeoutAt() {
        return timeoutAt;
    }

    public void setTimeoutAt(Date timeoutAt) {
        this.timeoutAt = timeoutAt;
    }

    public MessageType getClasses() {
        return classes;
    }

    public void setClasses(MessageType classes) {
        this.classes = classes;
    }

    public MessageDetailType getType() {
        return type;
    }

    public void setType(MessageDetailType type) {
        this.type = type;
    }

    public YesNoStatus getIsPush() {
        return isPush;
    }

    public void setIsPush(YesNoStatus isPush) {
        this.isPush = isPush;
    }

    public ReceiveType getReceiveType() {
        return receiveType;
    }

    public void setReceiveType(ReceiveType receiveType) {
        this.receiveType = receiveType;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
